package com.online.studyonline.controller;

import com.online.studyonline.pojo.Course;
import com.online.studyonline.pojo.Result;
import com.online.studyonline.pojo.ResultT;
import com.online.studyonline.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/study")
public class CourseController {
    @Autowired
    private CourseService courseService;


    @GetMapping("/getCourseName")
    public ResultT<List<String>> getCourseName(){

        return  courseService.getCourseName();
    }
    @GetMapping("/getCourseList")
    public Result getCourseList() {
        List<Course> courseList = courseService.list();
        return Result.success(courseList);
    }
    @GetMapping("/getCourseById")
    public Result getCourseById(@RequestParam("courseId") Integer courseId){
        Course course = courseService.getById(courseId);
        return Result.success(course);
    }
    @GetMapping("/findCourseList")
    public Result findCourseList(@RequestParam("courseFind") String courseFind) {
        List<Course> courseListTeacher = courseService.lambdaQuery().like(Course::getTeacherName, courseFind).list();
        if(courseListTeacher.size() == 0) {
            List<Course> courseListName = courseService.lambdaQuery().like(Course::getCourseName, courseFind).list();
            if(courseListName.size() == 0) {
                return Result.error("未找到相关课程");
            }
            return Result.success(courseListName);
        }
//        List<Course> courseList = new ArrayList<>();
//        courseService.lambdaQuery().like(Course::getCourseName, courseName).like(Course::getTeacherName, teacherName).list();
        return Result.success(courseListTeacher);
    }
}
